<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const props = defineProps({
  video: {
    type: Object,
    required: true
  },
  isFavorited: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['favorite-click'])

const favText = computed(() => {
  return props.isFavorited ? '已收藏' : '收藏'
})

function handleFavoriteClick() {
  emit('favorite-click')
}

function handleAuthorClick() {
  if (props.video.author) {
    // 编码作者名以确保URL安全
    const encodedAuthor = encodeURIComponent(props.video.author)
    router.push(`/author/${encodedAuthor}`)
  }
}
</script>

<template>
  <div class="video-info">
    <div class="title-line">
      <h1 class="title">{{ video.title }}</h1>
      <button :class="['fav', { active: isFavorited }]" @click="handleFavoriteClick">
        <span class="icon">{{ isFavorited ? '★' : '☆' }}</span>
        <span>{{ favText() }}</span>
      </button>
    </div>
    
    <div class="video-meta">
      <div class="stats">
        <span class="stat-item">
          <span class="icon">▶</span>
          {{ (video.plays ?? 0).toLocaleString() }} 播放
        </span>
        <span class="stat-item">
          <span class="icon">💬</span>
          {{ (video.danmaku ?? 0).toLocaleString() }} 弹幕
        </span>
        <span class="stat-item author-item" @click="handleAuthorClick" v-if="video.author">
          <span class="icon">👤</span>
          <span class="author-name">{{ video.author }}</span>
        </span>
      </div>
      
      <div class="tags">
        <span class="tag" v-for="tag in (video.tags || [])" :key="tag">{{ tag }}</span>
      </div>
    </div>
    
    <p class="desc">{{ video.description }}</p>
  </div>
</template>

<style scoped>
.video-info {
  margin-bottom: 32px;
  padding: 20px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  transition: all 0.3s ease;
}

.video-info:hover {
  box-shadow: var(--shadow-lg);
  border-color: rgba(99, 102, 241, 0.3);
}

.title-line {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 28px;
  font-weight: 800;
  line-height: 1.2;
  color: var(--fg);
  background: linear-gradient(135deg, var(--fg), var(--accent-light));
  -webkit-background-clip: text;
  background-clip: text;
  color: var(--fg);
}

.fav {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  font-weight: 600;
  font-size: 1rem;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.fav:hover {
  border-color: var(--accent);
  background: var(--surface-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
}

.fav.active {
  border-color: #f59e0b;
  background: #fef3c7;
  color: #92400e;
}

.fav .icon {
  font-size: 1.2em;
}

.video-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.stats {
  display: flex;
  gap: 24px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.95rem;
}

.author-item {
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 4px 8px;
  border-radius: var(--radius);
}

.author-item:hover {
  background: var(--surface-hover);
  color: var(--accent);
  transform: translateY(-1px);
}

.author-name {
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 4px;
  text-decoration-thickness: 2px;
  text-decoration-color: transparent;
  transition: all 0.3s ease;
}

.author-item:hover .author-name {
  text-decoration-color: var(--accent);
}

.stat-item .icon {
  font-size: 1.1em;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  padding: 4px 12px;
  background: var(--surface-hover);
  border-radius: 999px;
  font-size: 0.85rem;
  color: var(--accent);
  border: 1px solid rgba(99, 102, 241, 0.2);
  transition: all 0.3s ease;
}

.tag:hover {
  background: var(--accent);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(99, 102, 241, 0.2);
}

.desc {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
  font-size: 1rem;
  white-space: pre-line;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .title-line {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .fav {
    justify-content: center;
  }
  
  .video-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .stats {
    gap: 16px;
  }
}
</style>